$(document).ready(function(){    
    
    $('input').keyup(changeMade);
    $('select').change(changeMade);
    
    $('#cancel_button').click(function(){
        window.location = getBaseURL()+'settings/manage_staffusers';
    });
    
    $('#submit_button').click(function(){
        if (validateData()){
            postStaffuser();
        }
    });
    
    $("#input_username").keyup(function(){
        checkLogin($("#input_username").val(),$('#staffuser_id').val());
    });
    
    $("#input_email").keyup(function(){
        checkEmail($("#input_email").val(),$('#staffuser_id').val());
    });
    
    registerButtons();
});

function validateData(){
    var responses = new Object();
    var retval = true;
    if ($.trim($("#input_username").val()) == ""){
        responses["#username_section"] = "Username field is empty.";
        retval = false;
    }
    
    if ($.trim($("#input_email").val()) == ""){
        responses["#email_section"] = "Email field is empty.";
        retval = false;
    }
    
    if ($.trim($("#input_name").val()) == ""){
        responses["#name_section"] = "Name field is empty.";
        retval = false;
    }
    
    highlightSections(responses);
    
    return retval;
}

function postStaffuser(){
    var staffuser_id = $("#staffuser_id").val();
    var username = $("#input_username").val();
    var email = $("#input_email").val();
    var name = $("#input_name").val();
    var role = $("#role_options").val();
    var password = $.trim($("#input_password").text());
    var change = $("#change_password").length;
        
    $.post(getBaseURL()+"settings/create_staffuser/postStaffuser/",{
        staffuser_id: staffuser_id,
        username: username,
        email: email,
        name: name,
        role: role,
        password: password,
        change: change
    },
    function(data){
        if (data == "good"){
            saveMade();
            window.location = getBaseURL()+"settings/manage_staffusers/";
        }
        else if (data == "update"){
            saveMade();
            window.location = getBaseURL()+"settings/manage_staffusers/";
        }
        else if (data == "server"){
            saveMade();
            window.location = getBaseURL()+"settings/manage_staffusers/";
            
        }else{
            var responses = new Object();
            
            if (data.login){
                responses['#username_section'] = data.login;
            }

            if (data.email){
                responses['#email_section'] = data.email;
            }
            
            highlightSections(responses);
        }
    },"json");
}

function checkLogin(user_login, user_id){
    $.post(getBaseURL()+"settings/create_staffuser/checkLogin/",{
        staffuser_login: user_login,
        staffuser_id: user_id
    },
    function(data){
        var text = 'Not Unique';
        if (data == "good"){
            text = null;
        }
        toggleHighlight('#username_section', text);
    });
}

function checkEmail(user_email, user_id){
    $.post(getBaseURL()+"settings/create_staffuser/checkEmail/",{
        user_email: user_email,
        user_id: user_id
    },
    function(data){
        var text = 'Not Unique';
        if (data == "good"){
            text = null;
        }
        toggleHighlight('#email_section', text);
    });
}

function registerButtons(){
    $("#cancel_password").click(function(){
        $("#password_location").html('<input type="button" class="buttonLight" id="change_password" value="Reset Password"/>');
        registerButtons();
    });
    
    $("#change_password").click(function(){
        $("#password_location").html('<div id="input_password">'+$("#hidden_pass").attr('value')+'</div>'+
        '<input type="button" id="cancel_password" class="buttonLight" value="Cancel"/>');
    
        $("#input_password").keyup(function(){
            toggleHighlight('#password_section', null);
        });
        registerButtons();
    });
}